import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

public class EditZonePanel extends JPanel {
	public static final String NEW = "Tao moi ...";
	
	private static final long serialVersionUID = 1L;
	private AutoCompletePanel allZonePanel = null;
	private JLabel jLabel = null;
	private JPanel jPanel = null;
	private JLabel jLabel1 = null;
	private JButton updateButton = null;
	private JTextField nameTextField = null;

	private JButton deleteButton = null;

	/**
	 * This is the default constructor
	 */
	public EditZonePanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.setRows(1);
		this.setLayout(gridLayout);
		this.setSize(400, 200);
		this.add(getAllZonePanel(), null);
		this.add(getJPanel(), null);
	}
	
	
	private void initAllZonePanel() {
		ArrayList<Item> items = new ArrayList<Item>();
		ZoneManager zm = null;
		try {
			zm = ZoneManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<Zone> zones = zm.getAllZone();
		items.add(new Item(0,NEW));
		for (Zone station : zones) {
			Item item = new Item();
			item.id = station.getId();
			item.name = station.getName();
			items.add(item);
		}
		//tab Tim duong
		allZonePanel.setItems(items);
		allZonePanel.updateItemList();
	}

	/**
	 * This method initializes allZonePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private AutoCompletePanel getAllZonePanel() {
		if (allZonePanel == null) {
			jLabel = new JLabel();
			jLabel.setText("JLabel");
			allZonePanel = new AutoCompletePanel();
			allZonePanel.setLabel("Chọn khu vực: ");
			
			initAllZonePanel();	
			allZonePanel.getItemList().addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {
					Item item = getAllZonePanel().getSelectedItem();
					if (item == null) return;
					if (item.name.equals(NEW)) {
						getNameTextField().setText("");
					} else {
						getNameTextField().setText(item.name);
					}
				}
			});
		}
		return allZonePanel;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Tên: ");
			jLabel1.setBounds(new Rectangle(8, 51, 41, 24));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(jLabel1, null);
			jPanel.add(getNameTextField(), null);
			jPanel.add(getUpdateButton(), null);
			jPanel.add(getDeleteButton(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes updateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton("Cập nhật");
			updateButton.setBounds(new Rectangle(126, 111, 92, 30));
			
			updateButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Item item = getAllZonePanel().getSelectedItem();
					if (getNameTextField().getText().equals("")) {
						JOptionPane.showMessageDialog(null, "Chưa nhập tên");
						return;
					}
					ZoneManager zm = null;
					try {
						zm = ZoneManager.getInstance();
					} catch (SQLException e1) {
					}
					
					boolean result = false;
					if (item == null || item.name.equals(NEW)) {
						result = zm.addZone(new Zone(getNameTextField().getText()));
					} else {
						result = zm.updateZone(item.id, new Zone(nameTextField.getText()));
					}
					if (!result) {
						JOptionPane.showMessageDialog(null, "Hãy chọn tên khác.");
					}
					initAllZonePanel();
				}
			});
		}
		return updateButton;
	}

	/**
	 * This method initializes nameTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
			nameTextField.setBounds(new Rectangle(52, 51, 195, 23));
		}
		return nameTextField;
	}

	/**
	 * This method initializes deleteButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton();
			deleteButton.setBounds(new Rectangle(58, 111, 60, 28));
			deleteButton.setText("Xoá");
			
			deleteButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Item item = getAllZonePanel().getSelectedItem();
					if (item == null) return;
					ZoneManager zm = null;
					try {
						zm = ZoneManager.getInstance();
						zm.removeZone(item.id);
					} catch (SQLException e1) {
					}
					initAllZonePanel();
				}
			});
		}
		return deleteButton;
	}

}
